package com.dc.seckill.validator;

import com.dc.seckill.util.ValidatorUtil;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @模块 参数模块
 * @作者 djs
 * @时间 2018/3/18 22:37
 * @版本 V1.0
 * @描述 校验参数中手机号的校验器类
 */
public class IsMobileValidator implements ConstraintValidator<IsMobile, String>{

    private boolean required = false;

    @Override
    public void initialize(IsMobile constraintAnnotation) {
        required = constraintAnnotation.requeired();
    }

    /**
     * 校验手机号
     * @params [s, constraintValidatorContext]
     * @returns boolean
     */
    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if(required){
            return ValidatorUtil.isMobile(s);
        }else{
            if(StringUtils.isEmpty(s)){
                return true;
            }else{
                return ValidatorUtil.isMobile(s);
            }
        }
    }
}
